﻿Imports DevExpress.Web
Public Class AdminUpdateBorrow
    Inherits System.Web.UI.Page
    Dim mt As New MuonTra_BLL()
    Dim mtc As New MuonTra_DTO()
    Dim cb_kieumuon As ASPxComboBox
    Dim kieumuon As Boolean
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            loadDsMuon()
        Catch
        End Try

    End Sub

    Public Sub loadDsMuon()
        grid_dsmuon.DataSource = mt.loadDsMuon()
        grid_dsmuon.DataBind()

    End Sub



    Protected Sub grid_dsmuon_CustomColumnDisplayText(ByVal sender As Object, ByVal e As DevExpress.Web.ASPxGridViewColumnDisplayTextEventArgs) Handles grid_dsmuon.CustomColumnDisplayText
        If e.Column.FieldName = "kieumuon" Then
            If CBool(e.Value) Then
                e.DisplayText = "Mượn Về Nhà"
            Else
                e.DisplayText = "Mượn Tại Chỗ"
            End If
        End If
    End Sub



    Protected Sub grid_dsmuon_HtmlDataCellPrepared(ByVal sender As Object, ByVal e As DevExpress.Web.ASPxGridViewTableDataCellEventArgs) Handles grid_dsmuon.HtmlDataCellPrepared
        If e.DataColumn.FieldName = "ngaytra" Then
            Dim songayquahan As String = DateDiff(DateInterval.Day, Convert.ToDateTime(e.CellValue.ToString()), DateTime.Now)
            If songayquahan > 0 Then
                e.Cell.ForeColor = Drawing.Color.DarkRed
                e.Cell.Font.Bold = True
                e.Cell.ToolTip = "Số Ngày Quá Hạn: " + songayquahan
            End If
        End If
    End Sub


    Protected Sub grid_dsmuon_CustomSummaryCalculate(ByVal sender As Object, ByVal e As DevExpress.Data.CustomSummaryEventArgs) Handles grid_dsmuon.CustomSummaryCalculate
        Dim countQH As Integer
        For i As Integer = 0 To grid_dsmuon.VisibleRowCount - 1
            Dim dr As DataRow = grid_dsmuon.GetDataRow(i)
            If DateDiff(DateInterval.Day, dr("ngaytra"), DateTime.Now) > 0 Then
                countQH += 1
            End If
        Next
        e.TotalValue = "Tổng Số Sách Quá Hạn: " + countQH.ToString()
    End Sub




    Protected Sub grid_dsmuon_RowUpdating(ByVal sender As Object, ByVal e As DevExpress.Web.Data.ASPxDataUpdatingEventArgs) Handles grid_dsmuon.RowUpdating
        Dim getKieumuon As ASPxComboBox = TryCast(grid_dsmuon.FindEditRowCellTemplateControl(TryCast(grid_dsmuon.Columns("kieumuon"), GridViewDataColumn), "cb_kieumuon"), ASPxComboBox)
        Try
            mtc._idsach = e.Keys("id_sach")
            mtc._taikhoan = grid_dsmuon.GetRowValuesByKeyValue(e.Keys(grid_dsmuon.KeyFieldName), "taikhoan").ToString()
            mtc._tennvmuon = grid_dsmuon.GetRowValuesByKeyValue(e.Keys(grid_dsmuon.KeyFieldName), "tennv").ToString()
            If e.NewValues("ngaymuon").ToString() = String.Empty Then
                mtc._ngaymuon = e.OldValues("ngaymuon")
            Else
                mtc._ngaymuon = e.NewValues("ngaymuon")
            End If
            mtc._ngaymuoncu = e.OldValues("ngaymuon")
            If e.NewValues("ngaytra").ToString() = String.Empty Then
                mtc._ngaytra = e.OldValues("ngaytra")
            Else
                mtc._ngaytra = e.NewValues("ngaytra")
            End If
            mtc._ngaytracu = e.OldValues("ngaytra")
            mtc._kieumuon = getKieumuon.SelectedItem.Value
            mtc._kieumuoncu = e.OldValues("kieumuon")
            mt.capnhapMuon(mtc)
            e.Cancel = True
            grid_dsmuon.CancelEdit()
            loadDsMuon()
        Catch
        End Try

    End Sub

    Protected Sub grid_dsmuon_RowDeleting(ByVal sender As Object, ByVal e As DevExpress.Web.Data.ASPxDataDeletingEventArgs) Handles grid_dsmuon.RowDeleting
        Try
            mtc._idsach = e.Keys("id_sach")
            mtc._taikhoan = e.Values(1)
            mtc._ngaymuon = e.Values(2)
            mtc._ngaytra = e.Values(3)
            mtc._kieumuon = e.Values(4)
            mtc._tennvmuon = e.Values(5)
            mt.xoaMuon(mtc)
            e.Cancel = True
            loadDsMuon()
        Catch
        End Try

    End Sub

    Protected Sub grid_dsmuon_CellEditorInitialize(ByVal sender As Object, ByVal e As DevExpress.Web.ASPxGridViewEditorEventArgs) Handles grid_dsmuon.CellEditorInitialize
        kieumuon = grid_dsmuon.GetRowValues(e.VisibleIndex, "kieumuon")
    End Sub

    Protected Sub cb_kieumuon_Init(ByVal sender As Object, ByVal e As EventArgs)
        cb_kieumuon = TryCast(sender, ASPxComboBox)
        If CBool(kieumuon) Then
            cb_kieumuon.SelectedIndex = 0
        Else
            cb_kieumuon.SelectedIndex = 1
        End If
    End Sub
End Class